package com.vim.modules.web.utils;

/**
 * Java 代码自动生成工具
 */

public class StringUtil {
	private static String[] excludes = { "md", "auth" };

	private StringUtil() {}

	/**
	 * 将第一个字母转换为大写
	 */
	public static String setFirstCharUpcase(String s) {
		if (s == null || s.length() < 1)
			return s;
		char[] c = s.toCharArray();
		if (c.length > 0) {
			if (c[0] >= 'a' && c[0] <= 'z')
				c[0] = (char) ((short) (c[0]) - 32);
		}
		return String.valueOf(c);
	}

	public static String setFirstCharLowcase(String s) {
		if (s == null || s.length() < 1)
			return s;
		char[] c = s.toCharArray();
		if (c.length > 0) {
			if (c[0] >= 'A' && c[0] <= 'Z')
				c[0] = (char) ((short) (c[0]) + 32);
		}
		return String.valueOf(c);
	}

	public static boolean isEmpty(String a) {
		return a == null || a.trim().length() < 1;
	}

	public static String buildClassName(String tableName) {
		if (StringUtil.isEmpty(tableName)) {
			return "";
		}
		tableName = tableName.toLowerCase();
		String className = "";
		String[] words = tableName.split("[_\\.-]");
		for (int i = 0; i < words.length; i++) {
			if (i == 0) {
				if (includeIt(words[i])) {
					continue;
				}
				className = StringUtil.setFirstCharUpcase(words[i]);
			} else {
				className += StringUtil.setFirstCharUpcase(words[i]);
			}
		}
		return className;
	}

	public static String buildBeanName(String tableName) {
		if (StringUtil.isEmpty(tableName)) {
			return "";
		}
		tableName = tableName.toLowerCase();
		String className = "";
		String[] words = tableName.split("[_\\.-]");
		for (int i = 0; i < words.length; i++) {
			if (i == 0) {
				if (includeIt(words[i])) {
					continue;
				}
				className = StringUtil.setFirstCharLowcase(words[i]);
			} else {
				className += StringUtil.setFirstCharUpcase(words[i]);
			}
		}
		return className;
	}

	private static boolean includeIt(String t) {
		for (String s : excludes) {
			if (s.equals(t)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 根据列名创建java方法的field名称
	 * @param columnName
	 */
	public static String buildFieldName(String columnName) {
		if (StringUtil.isEmpty(columnName)) {
			return "";
		}
		columnName = columnName.toLowerCase();
		String fieldName = "";
		// 转换为帕斯卡（pascal）命名，首字母大写，每一个逻辑断点都有一个大写字母来标记
		String[] words = columnName.split("[_\\.-]");
		for (int i = 0; i < words.length; i++) {
			if (i == 0) {
				fieldName = words[i];
			} else {
				fieldName += StringUtil.setFirstCharUpcase(words[i]);
			}
		}
		return fieldName;
	}
}